package org.bigjava.buff.goods.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.bigjava.buff.common.upload.FileUpload;
import org.bigjava.buff.common.util.Page;
import org.bigjava.buff.goods.bean.Box;
import org.bigjava.buff.goods.bean.ExteriorAndPrice;
import org.bigjava.buff.goods.bean.Goods;
import org.bigjava.buff.goods.service.GoodsService;
import org.bigjava.buff.goods.service.TypeService;
import org.bigjava.buff.user.bean.User;
import org.bigjava.buff.user.service.DealService;
import org.bigjava.buff.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private UserService userService;
    @Autowired
    private DealService dealService;

    private List waitAddGoods_list = new ArrayList();


    //显示所有武器箱
    @RequestMapping("/showBox")
    public String goShowBox(HttpServletRequest request){

        List list = goodsService.getAllBox();

        request.setAttribute("boxList", list);

        return "goodspage/showBox";
    }

    //通过箱子id获得一个武器箱
    @RequestMapping("/getBox/{id}")
    public String getBox(@PathVariable Integer id,HttpServletRequest request){

        //传入箱子id去查询箱子中的物品
        List<Goods> list = goodsService.showGoodsInBox(id);
        request.setAttribute("goodsList", list);
        return "goodspage/openBox";
    }

    //通过箱子id获得一个武器箱
    @RequestMapping("/getBox2/{id}")
    public String getBox2(@PathVariable Integer id,HttpServletRequest request){

        //传入箱子id去查询箱子中的物品
        List list = goodsService.showGoodsInBox(id);
        request.setAttribute("goodsList", list);
        return "adminpage/boxContent";
    }

    //去添加一个箱子
    @RequestMapping("/gotoAddBox")
    public String gotoAddBox(HttpSession session){

        List list = typeService.findtypeByfid(0);

        //保存一级分类集合
        session.setAttribute("typeList",list);

        return "adminpage/addBox";
    }

    //去武器箱管理页面
    @RequestMapping("/gotoBoxManage")
    public String gotoBoxManage(HttpServletRequest request){

        request.setAttribute("allBoxList",goodsService.getAllBox());

        return "adminpage/boxManage";
    }

    //添加武器箱
    @RequestMapping("/addBox")
    public String addBox(@RequestParam(value = "file",required = false) MultipartFile file,Box box,HttpServletRequest
            request) throws Exception{

        String fileName = FileUpload.upload(file);
        box.setBoxImg("/upload/" + fileName);
        request.getSession().setAttribute("img", fileName);
        goodsService.addBox(waitAddGoods_list, box);
        request.getSession().setAttribute("allBoxList",goodsService.getAllBox());
        return "adminpage/boxManage";

    }

    //将要添加的饰品暂存到箱子中
    @RequestMapping(value = "/addGoodsInBox")
    public String addGoodsInBox(@RequestParam(value = "file",required = false) MultipartFile file,Goods goods,
                                HttpSession session) throws Exception{

        Goods fullGoods = typeService.fillGoodsType(goods);

        //上传文件
        String fileName = FileUpload.upload(file);
        //保存图片路径和名称
        if(fileName != null) fullGoods.setConverPath("/upload/" + fileName);

        waitAddGoods_list.add(fullGoods);

        session.setAttribute("waitAddGoods", waitAddGoods_list);
        return "adminpage/addBox";
    }
    //开箱
    @RequestMapping("/openBox")
    @ResponseBody
    public String openBox(Integer id, HttpSession session, HttpServletResponse response) throws Exception {

        Integer boxId = id;
        ObjectMapper objectMapper = new ObjectMapper();

        //获得当前登录用户信息
        User userNow = (User) session.getAttribute("userNow");

        //获取到要抽奖的用户余额
        User u =  userService.getUserbyId(userNow.getId());
        Double balance = u.getBalance();
        if(balance >= 16){
            //余额足够 进行抽取  余额-16
            userService.updateBalance(userNow.getId(), 16.0, -1);
            u.setBalance(u.getBalance() - 16);
            session.setAttribute("userNow", u);
            //抽取到的饰品
            ExteriorAndPrice ep =  goodsService.openBox(boxId,u.getId());
            String data = objectMapper.writeValueAsString(ep);
            return data;
        }

        //余额不足
        ServletOutputStream out =  response.getOutputStream();
        out.print("-1");

        return null;
    }

    //页面头部页面
    @RequestMapping("/head")
    public String headPage(){
        return "goodspage/head";
    }

    //页面尾部页面
    @RequestMapping("/tail")
    public String tailPage(){
        return "goodspage/tail";
    }

    //饰品市场页面
    @RequestMapping("/market")
    public String goJewelryMarketPage(HttpServletRequest request,Integer dealType,Integer pageNum){
        List list = (List) request.getSession().getAttribute("marketTypeList");
        if(list == null){
            List l = typeService.getAllType();
            request.getSession().setAttribute("marketTypeList", l);
        }
        Page page = new Page();

        page.setPageNum(pageNum);
        page.setPageSize(20);
        page = dealService.getDeal(dealType, page);

        request.setAttribute("dealPageInfo", page);

        return "goodspage/jewelryMarket";
    }

    //查看物品详情信息
    @RequestMapping("/goodsInfo/{id}")
    public String lookGoodsInfo(HttpServletRequest request,@PathVariable Integer id){

        System.out.println(id);

        //获得物品详情信息
        ExteriorAndPrice ep = goodsService.getGoodsInfo(id);
        request.setAttribute("ep", ep);

        //获得物品交易信息
        List dealList = dealService.getGoodsDealByGoodsId(ep.getGoods().getId(), 1);

        request.setAttribute("dealList", dealList);

        return "goodspage/goodsInfo";
    }
}
